package balmysundaycandy.morelowlevel.sample.controller.others;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import balmysundaycandy.morelowlevel.sample.controller.PathElement;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyRange;
import com.google.appengine.api.datastore.Transaction;

public class TwoController extends Controller {

	@Override
	protected Navigation run() {
		 DatastoreService service = DatastoreServiceFactory.getDatastoreService();
		 KeyRange parentKeyRange = service.allocateIds("parent", 1);
		 Key parentKey = parentKeyRange.getStart();
		 KeyRange childKeyRange = service.allocateIds(parentKey, "child", 1);
		 Key childKey = childKeyRange.getStart();
		  
		 @SuppressWarnings("unused")
		 Entity parent = new Entity(parentKey);
		 Entity child = new Entity(childKey);
		  
		 Transaction tx = service.beginTransaction();
		 service.put(tx, child);
		 tx.commit();
		  
		Entity child2 = null;
		try {
			child2 = service.get(childKey);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 tx = service.beginTransaction();
		 child2.setProperty("prop", "a");
		 service.put(tx, child2);
		 tx.commit();
		 return forward(PathElement.JSP_PATH + "/index.jsp");
	}

}
